﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;
using BaseSet.Entity.Common.Text;

namespace BaseSet.Entity.Common.DataOperation
{
    /// <summary>
    /// 列表辅助类
    /// </summary>
    public class ListHelper
    {
        /// <summary>
        /// 取列表的一部分
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="lst">要从中取数据的整个列表</param>
        /// <param name="start">起始 从哪一条开始</param>
        /// <param name="count">数目 取多少条</param>
        /// <returns></returns>
        public static IList<T> GetPartOfList<T>(IList<T> lst, int start, int count)
        {
            IList<T> lstResult = new List<T>();
            for (int lid = start; lid < start + count; lid++)
            {
                if (lid >= lst.Count)
                {
                    break;
                }
                lstResult.Add(lst[lid]);
            }
            return lstResult;
        }

        /// <summary>
        /// 获取源List中 给定字段的所有值 
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="list">源List</param>
        /// <param name="fName">给定字段</param>
        /// <returns></returns>
        public static ArrayList GetListItemByField<T>(IList<T> list, string fName)
        {
            Object value;
            ArrayList result = new ArrayList();
            for (int i = 0; i < list.Count; i++)
            {
                value = Tools.GetPropertyValue<T>(list[i], fName);
                if (value != null)
                {

                    result.Add(value);
                }
            }
            return result;
        }

        /// <summary>
        /// 获取源List中 给定字段的所有不重复的值
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="list">Ilist</param>
        /// <param name="fName">指定字段</param>
        /// <returns>ArrayList数组</returns>
        public static ArrayList GetListItemByFieldNoRepeat<T>(IList<T> list, string fName)
        {
            Object value;
            ArrayList result = new ArrayList();
            for (int i = 0; i < list.Count; i++)
            {
                value = Tools.GetPropertyValue<T>(list[i], fName);
                if (value != null)
                {
                    if (!result.Contains(value))
                    {
                        result.Add(value);
                    }
                }
            }
            return result;
        }

        /// <summary>
        /// 获取源List中 给定字段的值=给定值 的项
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="list">源List</param>
        /// <param name="fName">给定字段</param>
        /// <param name="value">给定值</param>
        /// <returns>Ilist</returns>
        public static IList<T> GetListItemByFieldValue<T>(IList<T> list, string fName, Object value)
        {
            Object propertyValue;
            IList<T> result = new List<T>();
            for (int i = 0; i < list.Count; i++)
            {
                propertyValue = Tools.GetPropertyValue<T>(list[i], fName);
                if (value.Equals(propertyValue))
                {
                    result.Add(list[i]);
                }
            }
            return result;
        }

        ///// <summary>
        ///// 列表排序 按指定字段 优化算法/使用比较器
        ///// </summary>
        ///// <typeparam name="T">类型</typeparam>
        ///// <param name="list">源List</param>
        ///// <param name="fName">给定字段</param>
        ///// <param name="isAsc">是否正序 true:正序 false:倒序</param>
        ///// <returns></returns>
        //public static IList<T> SortByField<T>(IList<T> list, string fName, bool isAsc)
        //{
        //    //List<T> lst = new List<T>();
        //    T tmpObj = null;
        //    for (int i = 0; i < list.Count; i++)
        //    {

        //    }
        //}

        ///// <summary>
        ///// 返回列表中某字段值最大的项
        ///// </summary>
        ///// <typeparam name="T">类型</typeparam>
        ///// <param name="list">源List</param>
        ///// <param name="fName">给定字段</param>
        ///// <returns></returns>
        //public static T GetMaxValueItem<T>(IList<T> list, string fName)
        //{
        //    if (list == null || list.Count == 0 || string.IsNullOrEmpty(fName))
        //    {
        //        return default(T);
        //    }

        //    int maxIndex = 0;
        //    object maxValue, curValue;

        //    for (int i = 1; i < list.Count; i++)
        //    {
        //        maxValue = Tools.GetPropertyValue<T>(list[maxIndex], fName);
        //        curValue = Tools.GetPropertyValue<T>(list[i], fName);
        //        if (curValue > maxValue)
        //        {
        //            maxIndex = i;
        //        }
        //    }

        //    return list[maxIndex];
        //}

        ///// <summary>
        ///// 返回列表中某字段值最小的项
        ///// </summary>
        ///// <typeparam name="T">类型</typeparam>
        ///// <param name="list">源List</param>
        ///// <param name="fName">给定字段</param>
        ///// <returns></returns>
        //public static T GetMinValueItem<T>(IList<T> list, string fName)
        //{
        //    if (list == null || list.Count == 0 || string.IsNullOrEmpty(fName))
        //    {
        //        return default(T);
        //    }

        //    int minIndex = 0;
        //    object minValue, curValue;

        //    for (int i = 1; i < list.Count; i++)
        //    {
        //        minValue = Tools.GetPropertyValue<T>(list[minIndex], fName);
        //        curValue = Tools.GetPropertyValue<T>(list[i], fName);
        //        if (curValue < minValue)
        //        {
        //            minIndex = i;
        //        }
        //    }

        //    return list[minIndex];
        //}

        /// <summary>
        /// 排序 临时 给定字段值为整形时有效  优化算法（使用比较器、排序字段值可以为多种类型、可以指定正序倒序）传入对象集的引用 如果排序失败 则原来的顺序也没有了
        /// </summary>
        /// <typeparam name="T">类型</typeparam>
        /// <param name="list">对象集</param>
        /// <param name="fName">给定字段 按这个字段 正序排序</param>
        /// <returns>返回是否成功</returns>
        public static bool SortList<T>(IList<T> list, string fName)
        {
            if (list == null || list.Count == 0)
            {
                return false;
            }

            //IList<T> sortList=new List<T>();

            T tmpFunc = default(T);
            int targetOrder = -1, compareOrder = -1;
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = i + 1; j < list.Count; j++)
                {
                    targetOrder = -1;
                    compareOrder = -1;
                    int.TryParse(Tools.GetPropertyValue(list[i], fName) + "", out targetOrder);
                    int.TryParse(Tools.GetPropertyValue(list[j], fName) + "", out compareOrder);
                    if(targetOrder==-1||compareOrder==-1)
                    {
                        return false;
                    }
                    if (targetOrder > compareOrder)
                    {
                        tmpFunc = list[i];
                        list[i] = list[j];
                        list[j] = tmpFunc;
                    }
                }
            }

            return true;
        }
    }
}
